package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.user.dto.AuthDto;
import com.heima.user.entity.ApAuthor;
import com.heima.user.entity.ApUser;
import com.heima.user.entity.ApUserRealname;
import com.heima.user.entity.WmUser;
import com.heima.user.feign.ArticleFeign;
import com.heima.user.feign.MediaFeign;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.IApUserRealnameService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.user.service.IApUserService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * APP实名认证信息表 服务实现类
 * </p>
 *
 * @author syl
 * @since 2021-12-28
 */
@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements IApUserRealnameService {

    @Autowired
    private IApUserService apUserService;

    @Override
    public ResponseResult listByStatus(AuthDto dto) {
        if(dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        LambdaQueryWrapper<ApUserRealname> queryWrapper = new LambdaQueryWrapper<>();
//        根据状态
        if(dto.getStatus()!=null){
            queryWrapper.eq(ApUserRealname::getStatus,dto.getStatus());
        }
//        根据状态分页查询

        IPage<ApUserRealname> page = new Page<>(dto.getPage(),dto.getSize());
        IPage<ApUserRealname> page1 = this.page(page, queryWrapper);
        return new PageResponseResult(dto.getPage(),dto.getSize(),page1.getTotal(),page1.getRecords());
    }

    @Override
    @GlobalTransactional
    public ResponseResult auth(AuthDto dto, int type) { //type1 通过  type 0 驳回
        ApUserRealname userRealname = this.getById(dto.getId());
        if(userRealname==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST);
        }

        if(type==0){ //type 0 驳回
//            只需要修改一下状态就可以了
            userRealname.setStatus(2); //审核失败
            userRealname.setReason(dto.getMsg());
            this.updateById(userRealname);
        }else{ //审核通过
//            创建自媒体人
//            再查询apUser
            ApUser apUser = apUserService.getById(userRealname.getUserId());
            WmUser wmUser = createWmUser(userRealname,apUser);
            if(wmUser!=null){
//               创建文章作者
                ApAuthor apAuthor =  createApAuthor(apUser,wmUser);
                if(apAuthor!=null){
                    wmUser.setApAuthorId(apAuthor.getId());
                    mediaFeign.updateWmUser(wmUser);
                }
            }

//           修改APUser
            apUser.setIsIdentityAuthentication(true); //是否已经实名认证
            apUser.setFlag(1);  //= "0 普通用户            1 自媒体人            2 大V"
            apUserService.updateById(apUser);
//            修改apRealNameuser
            userRealname.setStatus(9);
            this.updateById(userRealname);
        }

        return ResponseResult.okResult();
    }

    private ApAuthor createApAuthor(ApUser apUser, WmUser wmUser) {
        ApAuthor apAuthor = new ApAuthor();
        apAuthor.setName(apUser.getName());
        apAuthor.setType(2); //(value = "0 爬取数据            1 签约合作商            2 平台自媒体人            ")
        apAuthor.setUserId(apUser.getId());
        apAuthor.setWmUserId(wmUser.getId());
        ResponseResult<ApAuthor> responseResult = articleFeign.saveApAuthor(apAuthor);

        if (responseResult.getCode().equals(0)) {
            return responseResult.getData();
        }

        return null;
    }

    @Autowired
    private ArticleFeign articleFeign;

    @Autowired
    private MediaFeign mediaFeign;

    private WmUser createWmUser(ApUserRealname userRealname,ApUser apUser ) {

        WmUser wmUser = new WmUser();
        wmUser.setApUserId(userRealname.getUserId());
//        wmUser.setApAuthorId(); //文章作者id，等一会儿保存完文章作者后再赋值
        wmUser.setName(userRealname.getName());
        wmUser.setPassword(apUser.getPassword());
        wmUser.setSalt(apUser.getSalt());
        wmUser.setNickname(apUser.getName());
        wmUser.setImage(apUser.getImage());
//        wmUser.setLocation(apUser.getl); 没办法赋值 先空着
        wmUser.setPhone(apUser.getPhone());
        wmUser.setStatus(9); // 9 正常可用
//        wmUser.setEmail(apUser.get);  没办法赋值 先空着
        wmUser.setType(0); // "账号类型            0 个人             1 企业            2 子账号"
        wmUser.setScore(0);
//        wmUser.setLoginTime();   没办法赋值 先空着
        ResponseResult<WmUser> wmUserResponseResult = mediaFeign.saveWmUser(wmUser);

        if (wmUserResponseResult.getCode().equals(0)) {
            return wmUserResponseResult.getData();
        }

        return null;

    }
}
